package com.niconicod.ofb.utils;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;

public class PhysicsUtils {

    public static final float AIR_DENSITY = 1.2f;
    public static final float DRAG_COEFFICIENT = 1f;

    public static Body createPlaneBody(World world, float planeLength, float x, float y) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(x, y);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(planeLength / 4, planeLength / 2);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 1;
        fixtureDef.friction = 0.5f;
        fixtureDef.restitution = 0;

        Body body = world.createBody(bodyDef);
        body.createFixture(fixtureDef);
        System.out.println(body.getMass());
        return body;
    }

    public static Body createBoxBody(World world, Vector2 size, Vector2 position, float mass, float friction, float restitution) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(position);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(size.x / 2, size.y / 2);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = calculateDensity(size.x, size.y, mass);
        fixtureDef.friction = friction;
        fixtureDef.restitution = restitution;

        Body body = world.createBody(bodyDef);
        body.createFixture(fixtureDef);
        System.out.println(body.getMass());
        return body;
    }

    public static Body createBoxBody(World world, Vector2 size, Vector2 position, float mass) {
        return createBoxBody(world, size, position, mass, 0, 0);
    }

    public static float calculateDensity(float width, float height, float wantedMass) {
        // length is 1 in box2d
        float volume = width * height;
        return wantedMass /volume ;
    }
}
